package gui;

import java.awt.Dimension;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import stats.Profile;

import content.FileLoader;
import content.IOManage;
import content.GUIContent.*;

public class StatsGUI {
	public String DEFAULT_LOOK = UIManager.getSystemLookAndFeelClassName();
	public String theme = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel";
	
	public class MainWindowListener extends java.awt.event.WindowAdapter{
		
		public void windowClosing(WindowEvent e){
			int result = JOptionPane.showOptionDialog(null, "Would you like to save your Profile?", "Save", 
					JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
			if (result == JOptionPane.CLOSED_OPTION || result == 2){
				return;
			} else if(result == 0){
				File file = FileLoader.openFile(null);
				IOManage.saveFile(file);
				return;
			}
			e.getWindow().dispose();
		}
	}
	
	public void ShowGUI(){
		
		for(UIManager.LookAndFeelInfo s:UIManager.getInstalledLookAndFeels()){
			if (s.getClassName().equals(theme)){
				try {
					UIManager.setLookAndFeel(theme);
					break;
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (UnsupportedLookAndFeelException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		Profile profile = null;
		try {
			profile = new Profile();
		} catch (IOException e) {
			JFrame frame = new JFrame("Rappelz Stat Calculator - None");
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			JOptionPane.showMessageDialog(frame, "Could not load profile.", "Critical Error", JOptionPane.ERROR_MESSAGE);
			frame.dispose();
			return;
		}
		
		JFrame frame = new JFrame("Rappelz Stat Calculator - None");
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.addWindowListener(new MainWindowListener());
		
		frame.setBounds(400,100,0,0);
		frame.setJMenuBar(new GUIMenu().getMenu());
		try {
			frame.setContentPane(new GUIPanel().getPanel(profile));
		} catch (IOException e) {
			JOptionPane.showMessageDialog(frame, e.getMessage(), "Critical Error", JOptionPane.ERROR_MESSAGE);
			frame.dispose();
			return;
		}
		frame.setVisible(true);		
		frame.pack();
		frame.setSize(new Dimension(800, 600));
		frame.setResizable(false);
	}
	public static void main(String arg[]){
		javax.swing.SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				StatsGUI start = new StatsGUI();
				start.ShowGUI();
			}
		});
	}
}
